package com.cb.spzx.manager.service.impl;

import javax.servlet.http.HttpServletResponse;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cb.spzx.common.exception.CbException;
import com.cb.spzx.manager.listener.ExcelListener;
import com.cb.spzx.manager.service.CategoryService;
import com.cb.spzx.manager.mapper.CategoryMapper;
import com.cb.spzx.model.entity.product.Category;
import com.cb.spzx.model.vo.common.ResultCodeEnum;
import com.cb.spzx.model.vo.product.CategoryExcelVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
* @author lenovo
* @description 针对表【category(商品分类)】的数据库操作Service实现
* @createDate 2024-04-12 21:22:21
*/
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService{

    @Override
    public List<Category> findByParentId(Long parentId) {
        // 根据分类id查询它下面的所有的子分类数据
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getParentId,parentId).orderByDesc(Category::getId);
        List<Category> categoryList = baseMapper.selectList(queryWrapper);
        // 遍历分类的集合，获取每一个分类数据
        if (!CollectionUtils.isEmpty(categoryList)) {
            categoryList.forEach(category->{
                // 查询该分类下子分类的数量
                LambdaQueryWrapper<Category> queryWrapper1 = new LambdaQueryWrapper<>();
                queryWrapper1.eq(Category::getParentId,category.getId());
                Long count = baseMapper.selectCount(queryWrapper1);
                category.setHasChildren(count > 0);
            });
        }
        return categoryList;
    }

    @Override
    public void exportData(HttpServletResponse response) {
        try {
            // 设置响应结果类型
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("分类数据", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            // 查询数据库中的数据
            List<Category> categoryList = baseMapper.selectList(null);
            List<CategoryExcelVo> categoryExcelVoList = new ArrayList<>(categoryList.size());
            // 将从数据库中查询到的Category对象转换成CategoryExcelVo对象
            for(Category category : categoryList) {
                CategoryExcelVo categoryExcelVo = new CategoryExcelVo();
                BeanUtils.copyProperties(category, categoryExcelVo, CategoryExcelVo.class);
                categoryExcelVoList.add(categoryExcelVo);
            }
            // 写出数据到浏览器端
            EasyExcel.write(response.getOutputStream(),CategoryExcelVo.class)
                    .sheet("分类数据").doWrite(categoryExcelVoList);
        } catch (Exception e) {
            e.printStackTrace();
            throw new CbException(ResultCodeEnum.DATA_ERROR);
        }
    }

    @Override
    public void importData(MultipartFile file) {
        try {
            //创建监听器对象，传递mapper对象
            ExcelListener<CategoryExcelVo> excelListener = new ExcelListener<>(baseMapper);
            //调用read方法读取excel数据
            EasyExcel.read(file.getInputStream(),CategoryExcelVo.class,excelListener)
                    .sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
            throw new CbException(ResultCodeEnum.DATA_ERROR);
        }
    }
}




